package com.hskn.hss.module.sysinfo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.sysinfo.entity.SysInfo;
import com.hskn.hss.module.sysinfo.mapper.SysInfoMapper;
import com.hskn.hss.module.sysinfo.service.ISysInfoService;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 系统配置管理 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-04-30
 */
@Service
public class SysInfoServiceImpl extends ServiceImpl<SysInfoMapper, SysInfo> implements ISysInfoService {

    @Resource
    private SysInfoMapper sysInfoMapper;

    @Override
    public void mUpdate(JSONObject params) throws HssException {
        // TODO: 2021/5/4 校验是否有修改权限
        SysInfo sysInfo = JSON.parseObject(params.toJSONString(), SysInfo.class);
        sysInfo.setSysId("1");
        sysInfoMapper.updateById(sysInfo);
    }

    @Override
    public JSONObject getList(JSONObject params) throws HssException {
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        JSONObject json = new JSONObject();

        Map map = new HashMap();
        map.put("sysName", params.getString("sysName"));

        Page<SysInfo> inspectionPage = new Page<>(page, size, true);
        IPage<SysInfo> iPage = sysInfoMapper.listinspection(inspectionPage, map);

        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("inspectionlist", iPage.getRecords());
        return json;
    }
}
